package com.app.util;

import com.app.common.SystemConstant;
import com.app.vo.ValueAndNameVo;
import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CommonUtil {
    public static final double R = 6371; // 地球半径，单位：公里
    //根据最近N天获取时间列表
    public static List<String> getDayList(Integer day){
        List<String> dateList =new ArrayList<>();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < day; i++) {
            Date date = DateUtils.addDays(new Date(), -i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
        }
        return dateList;
    }
    //根据经纬度算距离
    public static Double getDistance(double lat1, double lon1, double lat2, double lon2) {
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);

        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
                * Math.sin(dLon / 2) * Math.sin(dLon / 2);

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        Double distance = R * c;
        distance = Math.pow(distance, 2);
        return Math.sqrt(distance);
    }
    //根据文件扩展名和 扩展名数组进行比较 获取扩展名类型
    public static Integer getFileType(String urlSuffix) {
        Integer type = 0;
        String[] imageSuffixArr = SystemConstant.IMAGE_SUFFIX.split(",");
        for (String image : imageSuffixArr) {
            if (urlSuffix.equals(image)) {
                type = 1;
            }
        }
        String[] videoSuffixArr = SystemConstant.VIDEO_SUFFIX.split(",");
        for (String video : videoSuffixArr) {
            if (urlSuffix.equals(video)) {
                type = 2;
            }
        }
        return type;
    }
}
